package com.chenke.yygh.controller;


import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenke.yygh.common.result.Result;
import com.chenke.yygh.common.utils.MD5;
import com.chenke.yygh.service.HospitalSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院上传接口")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")

public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    //1.查询医院设置表里的所有信息
    @GetMapping("findAll")
    @ApiOperation("查询全部数据")
    public Result findAllHospitalSet()
    {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    @DeleteMapping("{id}")
    @ApiOperation("根据id删除")
    public Result remmoveHospSet(@PathVariable Long id)
    {
        boolean b = hospitalSetService.removeById(id);

        if(b)
            {
                return   Result.ok();
            }
        else
            {
                return  Result.fail();
            }

    }


     @PostMapping("findPageHospSet/{current}/{limit}")
     @ApiOperation("分页带条件查询医院信息")
     //required = false 可以没有值
     public Result getPageSelect(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo)
     {
         //创建page对象
         Page<HospitalSet> page = new Page<>(current,limit);
         QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper();

         String hosname = hospitalSetQueryVo.getHosname();
         String hoscode = hospitalSetQueryVo.getHoscode();

         if (!StringUtils.isEmpty(hosname))
         {
             queryWrapper.like("hosname", hosname);
         }
         if (!StringUtils.isEmpty(hoscode))
         {
             queryWrapper.eq("hoscode",hoscode);
         }

         Page<HospitalSet> userPage = hospitalSetService.page(page,queryWrapper);

         return  Result.ok(userPage);
     }



     @PostMapping("saveHospitalSet")
     @ApiOperation("插入医院设置")
     public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet)
     {
         hospitalSet.setStatus(1);
         Random random = new Random();
         String encrypt = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000));
         hospitalSet.setSignKey(encrypt);
         boolean save = hospitalSetService.save(hospitalSet);
         return save?  Result.ok():  Result.fail();

     }



     @GetMapping("getHospSet/{id}")
     @ApiOperation("根据id获取医院设置")
     public Result getHospSet(@PathVariable Long id)
     {
         HospitalSet hp = hospitalSetService.getById(id);
         return Result.ok(hp);
     }




    @PutMapping("updateHospSet")
    @ApiOperation("修改医院设置")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet )
    {
        boolean b = hospitalSetService.updateById(hospitalSet);
        return b?  Result.ok():  Result.fail();
    }


    @DeleteMapping("batchDeleteHospSet")
    @ApiOperation("批量删除")
    public Result batchDeleteHospSet(@RequestBody  List<Long> id)
    {
        hospitalSetService.removeByIds(id);
        return Result.ok();
    }

}
